package com.example.finger.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.finger.R;
import com.example.finger.adpter.TaskInfoAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lyxmaster.lib.circledialog.CircleDialog;
import com.lyxmaster.lib.circledialog.callback.ConfigInput;
import com.lyxmaster.lib.circledialog.params.InputParams;
import com.lyxmaster.lib.circledialog.view.listener.OnInputClickListener;
import com.lyxmaster.lib.toasty.Toasty;
import com.lyxmaster.lib.view.BackActivity;
import com.lyxmaster.lib.xutils.view.annotation.ContentView;
import com.lyxmaster.lib.xutils.view.annotation.ViewInject;
import com.lyxmaster.lib.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LYX on 2018/11/27.
 * 添加挂锁页面
 */
@ContentView(R.layout.activity_add_task)
public class AddLockActivity extends BackActivity {
    @ViewInject(R.id.listview)
    private ListView listView;

    List<String> list = new ArrayList<>();
    TaskInfoAdapter adapter;
    Intent intent = new Intent();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);

        setTitle("添加挂锁");

        String data = getIntent().getStringExtra("data");
        list = new Gson().fromJson(data, new TypeToken<List<String>>(){}.getType());
        intent.putExtra("data", new Gson().toJson(list));
        setResult(Activity.RESULT_OK, intent);

        adapter = new TaskInfoAdapter(context,list, 70);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
                new CircleDialog.Builder(AddLockActivity.this)
                        .setCanceledOnTouchOutside(true)
                        .setCancelable(true)
                        .setTitle("删除挂锁")
                        .setText("确定要删除 " + list.get(i) + " 挂锁吗？")
                        .setNegative("取消", null)
                        .setPositive("确定", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Toasty.success(context, "删除成功").show();
                                list.remove(i);
                                intent.putExtra("data", new Gson().toJson(list));
                                adapter.notifyDataSetChanged();
                            }
                        }).show();
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    //当用户点击Action按钮的时候，系统会调用Activity的onOptionsItemSelected()方法
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.menu_add:
                add();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void add(){
        new CircleDialog.Builder()
                .setCanceledOnTouchOutside(true)
                .setCancelable(true)
                .setTitle("添加挂锁号码")
                .configInput(new ConfigInput() {
                    @Override
                    public void onConfig(InputParams params) {
                        params.gravity = Gravity.CENTER;
                        params.inputType = InputType.TYPE_CLASS_NUMBER;
                    }
                })
                .setNegative("取消", null)
                .setPositiveInput("确定", new OnInputClickListener() {
                    @Override
                    public void onClick(String text, View v) {
                        list.add(text);
                        intent.putExtra("data", new Gson().toJson(list));
                        adapter.notifyDataSetChanged();
                    }
                })
                .showWithkeyboard(this);
    }
}
